package com.global.model;

import java.io.Serializable;
import javax.persistence.*;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import com.global.comparator.QueryParameterDefinitionComparator;

import java.util.ArrayList;
import java.util.List;

/**
 * The persistent class for the ZLY_ST_BSC_DSOX database table.
 * 
 * @author zly601436
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "ZLY_ST_BSC_DSOX")
@SequenceGenerator(name = "ZLY_ST_BSC_DSOX_ID_GENERATOR", sequenceName = "ZLY_ST_BSC_DSOX_ID_SEQ", initialValue=100, allocationSize = 1)
@NamedQueries({
        @NamedQuery(
                name = "findDataSourceXByName",
                query = "select d from DataSourceX d where upper(d.name) = :uniqueName "
        )   
})
public class DataSourceX extends BaseObject implements Serializable {

	private static final long serialVersionUID = -1511060241626241422L;

	private Long id;
	private String name;
	private String sql;
	private String description;
    private String queryType;
	
	private List<DataSourceXColumn> dataSourceXColumns;
	private List<QueryParameterDefinition> queryParameterDefinitions;

    /**
     * 定义操作人、操作时间
     */
    private Who who;
    
	public DataSourceX() {

	}

	public DataSourceX(String name, String description, String sql, String queryType) {
		this.name = name;
		this.description = description;
		this.sql = sql;
		this.queryType = queryType;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ZLY_ST_BSC_DSOX_ID_GENERATOR")
	public Long getId() {
		return this.id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 返回数据源的名称
	 * 
	 * @return
	 */
	@Column(nullable = false, length = 128, unique = true)
	public String getName() {
		return this.name;
	}
	/**
	 * 设置数据源的名称
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = (name==null ? "" : name.trim().toUpperCase());
	}

	/**
	 * 数据源的描述
	 * 
	 * @return
	 */
	@Column(nullable = false, length = 1024)
	public String getDescription() {
		return this.description;
	}
	/**
	 * 设置数据源的描述
	 * 
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = (description==null ? "" : description.trim());
	}

	/**
	 * 获取数据源对应的SQL查询语句(懒加载)
	 * 
	 * @return String
	 */
	@Basic(fetch = FetchType.LAZY)
	@Column(columnDefinition = "CLOB", nullable = false)
	public String getSql() {
		return this.sql;
	}
	/**
	 * 设置数据源对应的SQL查询语句
	 * 
	 * @param sql
	 */
	public void setSql(String sql) {
		this.sql = (sql==null ? "" : sql.trim().toLowerCase());
	}

	// bi-directional many-to-one association to DataSouceXColumn
	/**
	 * 获取数据源中的字段清单
	 */
	@OneToMany(mappedBy = "dataSourceX")
	public List<DataSourceXColumn> getDataSourceXColumns() {
		return this.dataSourceXColumns;
	}
	/**
	 * 设置数据源中的字段清单
	 * 
	 * @param dataSourceXColumns
	 */
	public void setDataSourceXColumns(List<DataSourceXColumn> dataSourceXColumns) {
		this.dataSourceXColumns = dataSourceXColumns;
	}
	
	// bi-directional many-to-one association to QueryParameterDefinition
	/**
	 * 获取数据源中的参数（固定参数以及过滤数据参数）清单，并且按照参数顺序升序排列
	 */
	@OneToMany(mappedBy = "dataSourceX")
	@Sort(type=SortType.COMPARATOR,comparator=QueryParameterDefinitionComparator.class)
	public List<QueryParameterDefinition> getQueryParameterDefinitions() {
		return this.queryParameterDefinitions;
	}
	public void setQueryParameterDefinitions(List<QueryParameterDefinition> queryParameterDefinitions) {
		this.queryParameterDefinitions = queryParameterDefinitions;
	}
	
	/**
	 * 获取固定参数清单，并且按照参数顺序升序排列
	 * @return
	 */
	@Transient
	public List<QueryParameterDefinition> getFixedParameterDefinitions() {
		List<QueryParameterDefinition> list = new ArrayList<QueryParameterDefinition>();
		for(QueryParameterDefinition q : queryParameterDefinitions){
			if(q.isFixed()){
				list.add(q);
			}
		}
		return list;
	}

	/**
	 * 获取用于过滤数据的参数清单，并且按照参数顺序升序排列
	 * @return
	 */
	@Transient
	public List<QueryParameterDefinition> getFilterParameterDefinitions() {
		List<QueryParameterDefinition> list = new ArrayList<QueryParameterDefinition>();
		for(QueryParameterDefinition q : queryParameterDefinitions){
			if(q.isFilter()){
				list.add(q);
			}
		}
		
		return list;
	}
	
	/**
	 * 返回数据源的SQL查询类型
	 * {@link com.global.vo.QueryType}
	 * @return
	 */
	@Column(nullable=false)
	public String getQueryType(){
		return queryType;
	}
	public void setQueryType(String queryType){
		this.queryType = queryType;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}

		DataSourceX other = (DataSourceX) obj;

		if (id != other.id) {
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equalsIgnoreCase(other.name)) {
				return false;
			}
		}

		return true;
	}

	@Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("name", this.name)
                .append("description", this.description)
                .append("queryType", this.queryType).toString();
    }
}